package com.foolishbird.sdk.ui.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.params.RedBalanceResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.RedConfig;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.ui.view.NoScrollGridView;
import com.foolishbird.sdk.utils.MyFragmentManger;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.WL;


public class RedPackageTixianFragment extends BaseHttpFragment implements View.OnClickListener {



    private TextView titleTxt,moneyText,checkText,totalText;
    private ImageView backView;
    private MyAdapter myAdapter;
    private EditText moneyEdit;
    private LinearLayout anyLayout,limitLayout,weixinLayout,zhfLayout;
    private NoScrollGridView mGridView;

    public static final RedPackageTixianFragment newInstance(Bundle bundle) {
        RedPackageTixianFragment fragment = new RedPackageTixianFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    private RedConfig redConfig;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_fragment_redpackage_tixian");
        View contentView =inflater.inflate(resource,null);
        backView = contentView.findViewById(getViewIdentifier("fb_fragment_login_back"));
        backView.setOnClickListener(this);
        titleTxt = contentView.findViewById(getViewIdentifier("fb_fragment_float_title"));
        titleTxt.setText("红包提现");
//        mListview = contentView.findViewById(getViewIdentifier("float_notice_list"));
        moneyText = contentView.findViewById(getViewIdentifier("redpackage_tixian_money"));
        checkText = contentView.findViewById(getViewIdentifier("redpackage_tixian_binded"));
        checkText.setOnClickListener(this);
        totalText = contentView.findViewById(getViewIdentifier("redpackage_tixian_totalText"));
        totalText.setOnClickListener(this);
        totalText = contentView.findViewById(getViewIdentifier("redpackage_tixian_totalText"));
        moneyEdit = contentView.findViewById(getViewIdentifier("redpackage_tixian_moneyEdit"));
        moneyEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                    money = moneyEdit.getText().toString().trim();
            }
        });
        weixinLayout = contentView.findViewById(getViewIdentifier("redpackage_tixian_wechat"));
        zhfLayout = contentView.findViewById(getViewIdentifier("redpackage_tixian_alipay"));
        weixinLayout.setOnClickListener(this);
        zhfLayout.setOnClickListener(this);
        anyLayout = contentView.findViewById(getViewIdentifier("redpackage_tixian_anyLayout"));
        limitLayout = contentView.findViewById(getViewIdentifier("redpackage_tixian_limitLayout"));
        redConfig = (RedConfig)getArguments().getSerializable("config");
        if(redConfig.getAmountType()==1){
            anyLayout.setVisibility(View.VISIBLE);
            limitLayout.setVisibility(View.GONE);
        }else{
            anyLayout.setVisibility(View.GONE);
            limitLayout.setVisibility(View.VISIBLE);
        }
        mGridView = contentView.findViewById(getViewIdentifier("redpackage_tixian_gridview"));
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                money = redConfig.getWithdrawAmountOptions()[i];
                myAdapter.notifyDataSetChanged();
            }
        });
        myAdapter = new MyAdapter(redConfig.getWithdrawAmountOptions());
        mGridView.setAdapter(myAdapter);
        HttpEngineApi.getInstance().redpackageUserInfo(requestCallbackHandler(RedBalanceResponseParams.class));
        return contentView;

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            HttpEngineApi.getInstance().redpackageUserInfo(requestCallbackHandler(RedBalanceResponseParams.class));
            moneyEdit.setText("");
            money = null;
            myAdapter.notifyDataSetChanged();
        }
    }

    private String money;

    private RedBalanceResponseParams.RedBalanceResponseData myData;

    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);
        if (response != null) {
            int code = response.getState().getCode();
            if (State.SUCCESS == code) {
                if(response instanceof RedBalanceResponseParams){
                    myData =((RedBalanceResponseParams)response).getData();
                    setView();
                }
//                else if(flag == RedpackageTixianNowFragment.TaskId){
//                    moneyEdit.setText("");
//                    money = null;
//                }
            }
//            setAdapter();
        }
        WL.d("FloatNoticeFragment",response.toString());
    }




    private void setView(){
        moneyText.setText(myData.getBalance().getCurrent_balance());
//        RedEntrance entrance = myData.getEntrance();
//        if(entrance.getCashRedEntrance()==1){
//            ((LinearLayout)getRedPackageText.getParent()).setVisibility(View.GONE);
//        }
//        if(entrance.getNormalRedEntrance()==1){
//            ((LinearLayout)levelRedpackageText.getParent()).setVisibility(View.GONE);
//        }
//        if(entrance.getSendRedEntrance()==1){
//            confirmBtn.setVisibility(View.GONE);
//        }
//
//        RedUserInfo userInfo = myData.getUserinfo();
//        Picasso.with(getContext()).load(userInfo.getAvatar()).transform(new CircleTransform()).into(avatarImage);
//        accountText.setText(userInfo.getName());
//        nameText.setText(userInfo.getRoleName());
//        levelText.setText(userInfo.getRoleLevel()+"级");
//        timeText.setText("("+userInfo.getRoleCreated()+")");
//        moneyText.setText(userInfo.getBalance()+"元");
////        RedCash cash =new RedCash();
////        cash.setCreatedAt("09-06 18:35:46");
////        cash.setAmount("23.23");
////        cash.setPrefixTitle("恭喜玩家【lcm***893】获得现金红包");
////        cash.setSuffixTitle("元");
////
////        RedCash cash2 =new RedCash();
////        cash2.setAmount("4.44");
////        cash2.setCreatedAt("09-06 18:35:46");
////        cash2.setPrefixTitle("恭喜玩家【ttttt】获得现金红包");
////        cash2.setSuffixTitle("元");
//        List<RedCash> cashes=new ArrayList<>();
////        cashes.add(cash);
////        cashes.add(cash2);
//        scollTextView.setData(myData.getRedNews()==null?cashes:myData.getRedNews());
//        myAdapter.setCashs(myData.getRedCash());
    }



    class ViewHolder {
        TextView moneyText;
    }



    @Override
    public void onClick(View view) {
        if(view ==backView){
            MyFragmentManger.getInstance().goBack(this);
        }
        else if (view == checkText){
//            MyFragmentManger.getInstance().gotoFragment(RedpackageSendFragment.newInstance(null));
            MyFragmentManger.getInstance().gotoFragment(RedpackageBindedFragment.newInstance(null));
        }else if(view == zhfLayout){
            if(TextUtils.isEmpty(money)){
                if (redConfig.getAmountType()==1){
                    Toast.makeText(getContext(),"请先输入提现金额",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(getContext(),"请先选择提现金额",Toast.LENGTH_SHORT).show();
                }
            }else if(myData.getStatus().getZfb()==0) {
//                zhfLayout.setBackgroundResource(ResUtils.getDrawableIdentifier(getContext(),"fb_redpckage_type_select"));
//                weixinLayout.setBackgroundResource(ResUtils.getDrawableIdentifier(getContext(),"fb_redpckage_type_nomal"));
                MyFragmentManger.getInstance().gotoFragment(RedpackageAlipayFragment.newInstance(null));
            }else{
                zhfLayout.setBackgroundResource(ResUtils.getDrawableIdentifier(getContext(),"fb_redpckage_type_select"));
                weixinLayout.setBackgroundResource(ResUtils.getDrawableIdentifier(getContext(),"fb_redpckage_type_nomal"));
                Bundle bundle = new Bundle();
                bundle.putInt("type",2);
                bundle.putString("money",money);
                MyFragmentManger.getInstance().gotoFragment(RedpackageTixianNowFragment.newInstance(bundle));
            }
        }else if (view ==weixinLayout){
            if(TextUtils.isEmpty(money)) {
                if (redConfig.getAmountType() == 1) {
                    Toast.makeText(getContext(), "请先输入提现金额", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getContext(), "请先选择提现金额", Toast.LENGTH_SHORT).show();
                }
            }else if(myData.getStatus().getWx()==0){
                MyFragmentManger.getInstance().gotoFragment(RedpackageWeChatFragment.newInstance(null));
            }else{
                weixinLayout.setBackgroundResource(ResUtils.getDrawableIdentifier(getContext(),"fb_redpckage_type_select"));
                zhfLayout.setBackgroundResource(ResUtils.getDrawableIdentifier(getContext(),"fb_redpckage_type_nomal"));
                Bundle bundle = new Bundle();
                bundle.putInt("type",1);
                bundle.putString("money",money);
                MyFragmentManger.getInstance().gotoFragment(RedpackageTixianNowFragment.newInstance(bundle));
            }
        }else if(view == totalText){
            moneyEdit.setText(myData.getBalance().getCurrent_balance());
        }
//        else if(view == confirmBtn){
//            Bundle bundle = new Bundle();
//            bundle.putSerializable("config",myData.getRedConfig());
//            MyFragmentManger.getInstance().gotoFragment(RedpackageSendFragment.newInstance(bundle));
//        }else if(view == detailText){
//            MyFragmentManger.getInstance().gotoFragment(RedpackageDetailFragment.newInstance(null));
//        }
//        OnFinishActivityListener listener = getOnFinishActivityListener();
//        if (listener != null) {
//            listener.doFinishActivity(this);
//        }
    }




    class MyAdapter extends BaseAdapter{

        private String[] cashs;
        public MyAdapter(String[] cashs){
            this.cashs = cashs;
        }

        public void setCashs(String[] cashs){
            this.cashs = cashs;
            notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            return cashs==null?0:cashs.length;
        }

        @Override
        public Object getItem(int i) {
            return cashs[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if (view == null) {
                int resource = ResUtils.getLayoutIdentifier(getContext(), "fb_redpackage_tixian_item");
                view = LayoutInflater.from(getContext()).inflate(resource, null);
                holder = new ViewHolder();
                holder.moneyText = (TextView) view.findViewById(ResUtils.getViewIdentifier(getContext(), "redpackage_tixian_item_money"));

                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
//            RedCash menu =cashs.get(i);
            holder.moneyText.setText(cashs[i]+"元");
            if(!TextUtils.isEmpty(money)&&money.equals(cashs[i])){
                holder.moneyText.setTextColor(Color.parseColor("#e63624"));
            }else {
                holder.moneyText.setTextColor(Color.parseColor("#505050"));
            }
//            holder.prefixText.setText(menu.getPrefixTitle());
//            holder.moneyText.setText(menu.getAmount());
//            holder.suffixText.setText(menu.getSuffixTitle());
            return view;
        }
    }

}
